package com.hyzh.latte.vehicle.socket.handler;

import cn.hutool.core.util.StrUtil;
import com.coffee.socketio.CoffeeSocketioException;
import com.coffee.socketio.CoffeeSocketioExecutor;
import com.coffee.socketio.command.TextCommand;
import com.coffee.socketio.handler.CoffeeHandler;
import com.coffee.socketio.handler.SocketioTextHandler;
import com.hyzh.latte.vehicle.dmo.PCSwitchCameraMessage;
import com.hyzh.latte.vehicle.nezha.NezhaClient;
import com.hyzh.latte.vehicle.nezha.NezhaPersonManager;
import com.hyzh.latte.vehicle.nezha.NezhaSessionManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

/**
 * 前端 切换 摄像头信息
 * @author: songkui
 * @since: 2024/3/21 0:14
 */
@CoffeeHandler("CommandType.PcSwitch")
@Service
@Slf4j
public class ClientSwitchHandler implements SocketioTextHandler<PCSwitchCameraMessage> {


    @Override
    public void handle(TextCommand<PCSwitchCameraMessage> command) throws CoffeeSocketioException {
        PCSwitchCameraMessage message = command.getRequest();
        if (null == message || StrUtil.isEmpty(message.getSwitchType())){
            log.error("Pc Switch Type: message is null; [sessionId: {}] ", command.getClient().getSessionId());
            return;
        }

        NezhaClient nezhaClient = NezhaSessionManager.getInstance().getNezhaClient(command.getSessionId());
        if (null == nezhaClient){
            log.error("Pc Switch Type:  Login failure [sessionId: {}] ", command.getClient().getSessionId());
            command.getClient().disconnect();
            return;
        }
        log.info("Pc Switch Camera:[sid: {}, uid: {}, old camera: {}, new camera: {}] ", nezhaClient.getSessionId(), nezhaClient.getUserId(), nezhaClient.isAllCamera() ? "ALL" :  null != nezhaClient.getCameras() ? String.join(",", nezhaClient.getCameras()) : "null",  message.getSwitchType());
        Executor executor = CoffeeSocketioExecutor.getInstance().getDisruptor(nezhaClient.getParkCode().hashCode());
        executor = null == executor ? CoffeeSocketioExecutor.getInstance().getDefaultExecutor() : executor;
        executor.execute(() ->{
            String switchType = message.getSwitchType();
          // 【V_CLOS：车辆轨迹关闭； P_ON：人员轨迹开启；    P_CLOSE：人员轨迹关闭】
            switch (switchType){
                case "ALL" :
                    NezhaSessionManager.getInstance().switchCamera(nezhaClient, null, true);
                    nezhaClient.setAllCamera(true);
                    nezhaClient.setVcs(true);
                    break;

                case "V_CLOS":
                    NezhaSessionManager.getInstance().removeClient(nezhaClient, false);
                    nezhaClient.setVcs(false);
                    break;

                case "P_ON" :
                    nezhaClient.setPcs(true);
                    NezhaPersonManager.getInstance().onConnect(nezhaClient);
                    break;

                case "P_CLOSE":
                    nezhaClient.setPcs(false);
                    NezhaPersonManager.getInstance().onCols(nezhaClient);
                    break;

                default:
                    Set<String> newCameraCode = Arrays.stream(message.getSwitchType().trim().split(","))
                            .map(String::trim)
                            .collect(Collectors.toSet());
                    NezhaSessionManager.getInstance().switchCamera(nezhaClient, newCameraCode, false);
                    nezhaClient.setCameras(newCameraCode);
                    nezhaClient.setAllCamera(false);
                    nezhaClient.setVcs(true);

            }

        });

    }


}
